package com.gitee.srurl.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gitee.srurl.core.annotation.Login;
import com.gitee.srurl.core.domain.SucBlacklistDomain;
import com.gitee.srurl.core.service.ISucBlacklistService;
import com.gitee.srurl.core.util.PageQuery;
import com.gitee.srurl.core.util.valid.AddGroup;
import com.gitee.srurl.core.util.valid.UpdateGroup;
import com.gitee.srurl.system.util.PageUtil;
import com.gitee.srurl.system.util.response.PageResponse;
import com.gitee.srurl.system.util.response.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 生成短链黑名单
 *
 * @author zxw
 * @date 2023/5/3 11:29
 */
@RestController
@RequestMapping("blacklist")
@RequiredArgsConstructor
public class BlackListController {

    private final ISucBlacklistService blacklistService;

    @Login
    @GetMapping("list")
    public R<PageResponse<SucBlacklistDomain>> list(PageQuery pageQuery, SucBlacklistDomain blacklistDomain) {

        IPage<SucBlacklistDomain> page = blacklistService.getList(pageQuery, blacklistDomain);
        return R.page(PageUtil.mybatisPageTransfer(page));
    }

    @Login
    @PutMapping("{id}")
    public R<Void> update(@PathVariable Long id, @RequestBody @Validated(UpdateGroup.class) SucBlacklistDomain blacklistDomain) {

        blacklistDomain.setId(id);
        blacklistService.updateBlacklist(blacklistDomain);
        return R.ok();
    }

    @Login
    @PostMapping
    public R<Void> add(@RequestBody @Validated(AddGroup.class) SucBlacklistDomain blacklistDomain) {

        blacklistService.add(blacklistDomain);
        return R.ok();
    }

    @Login
    @DeleteMapping("{id}")
    public R<Void> add(@PathVariable Long id) {

        blacklistService.delete(id);
        return R.ok();
    }
}
